﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;
using CardsServer.Tools;
namespace CardsServer.WCF
{
    public partial interface IService
    {




        [OperationContract]
        List<View_whd_d_0> View_whd_d_0_get_pid(string pid, DateTime? sd, DateTime? ed, Guid? infoempid, int? state, int ACID);

        [OperationContract]
        List<View_whd_d_1> View_whd_d_1_get_pid(string pid, DateTime? sd, DateTime? ed, int? state, int? did, int ACID);
        
        [OperationContract]
        string afterprintwirtefk(List<int> dids, List<Guid> empids, int ACID);

        [OperationContract]
        string whd_add(whd_M m, List<View_whd_d_0> ds, int ACID);

        [OperationContract]
        string ld(whd_d_fk fk, int state, int ACID);

        [OperationContract]
        List<get_whd_View_allResult> getwhd_view_allresult(string ldrname, int state, int ACID);

        [OperationContract]
        List<get_whd_View_all_bydateResult> get_whd_View_all_bydateResult(int state, DateTime sd,DateTime ed,int ACID);
    }
    public partial class Service : IService
    {


        public string whd_add(whd_M m, List<View_whd_d_0> ds, int ACID)
        {
            string r = "";
            using (TransactionScope tc = new TransactionScope())
            {
                try
                {
                    using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                    {

                        string r1 = whd_M_Add(m, ACID, ctx);
                         if (!string.IsNullOrEmpty(r1))
                         {
                             throw new Exception(r1);
                         }
                        foreach (View_whd_d_0 vd in ds)
                        {
                            whd_d d = new whd_d();
                            d.pid = m.pid;
                            d.ClientName=vd.用户名称;
                            d.slno = vd.受理单号;
                            d.state = 0;
                            d.wnjf = vd.往年缴费;
                            d.jrhye = vd.实时余额;
                            d.Remarks = vd.备注信息;
                            string r2 = whd_d_Add(d, ACID,ctx);

                            if (!string.IsNullOrEmpty(r2))
                            {
                                throw new Exception(r2);
                            }
                            
                        }
                        ctx.SubmitChanges();
                        tc.Complete();
                    }
                }
                catch (Exception ex)
                {
                    r = ex.Message;
                }
            }
            return r;
        }

        public List<View_whd_d_0> View_whd_d_0_get_pid(string pid, DateTime? sd, DateTime? ed, Guid? infoempid, int? state, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                Expression<Func<View_whd_d_0, bool>> predicate = PredicateBuilder.True<View_whd_d_0>();

                if (!string.IsNullOrEmpty(pid))
                {
                    predicate = predicate.And(p => p.pid == pid);
                }
                if (sd!=null &&ed != null)
                {
                   predicate = predicate.And(p=>p.录入日期>=sd &&p.录入日期<=ed);
                }
                if (infoempid!=null)
                {
                    if ((!infoempid.Equals(Guid.Empty)))
                    {
                        predicate = predicate.And(p => p.info_OPid == infoempid.Value);
                    }
                }
                if (state.HasValue)
                {
                   predicate = predicate.And(p => p.state == state.Value);
                }
                return ctx.View_whd_d_0.Where(predicate).ToList();
            }
        }

        public List<View_whd_d_1> View_whd_d_1_get_pid(string pid, DateTime? sd, DateTime? ed, int? state,int? did, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                Expression<Func<View_whd_d_1, bool>> predicate = PredicateBuilder.True<View_whd_d_1>();

                if (!string.IsNullOrEmpty(pid))
                {
                    predicate = predicate.And(p => p.pid == pid);
                }
                if (sd != null && ed != null)
                {
                    predicate = predicate.And(p => p.录入日期 >= sd && p.录入日期 <= ed);
                }
                if(state!=null)
                {
                    predicate = predicate.And(p => p.state == state);
                }
                if (did != null)
                {
                    predicate = predicate.And(p => p.did == did);
                }
                return ctx.View_whd_d_1.Where(predicate).ToList();
            }
        }

        public string afterprintwirtefk(List<int> dids, List<Guid> empids, int ACID)
        {
            string r = "";
            using (TransactionScope tc = new TransactionScope())
            {
                try
                {
                    using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                    {
                        for (int i = 0; i < dids.Count; i++)
                        {
                            if (ctx.whd_d_fk.Count(p=>p.did==dids[i]) > 0)
                            {
                                ctx.whd_d_fk.Single(p=>p.did==dids[i]).whr=empids[i];
                            }
                            else
                            {
                                whd_d_fk fk=new whd_d_fk();
                                fk.did=dids[i];
                                fk.whr=empids[i];
                                whd_d_fk_Add(fk, ACID, ctx);
                            }
                            ctx.whd_d.Single(p => p.did == dids[i]).state = 2;
                        }
                        ctx.SubmitChanges();
                        tc.Complete();

                    }
                }
                catch (Exception ex)
                {
                    r = ex.Message;
                }
            }
            return r;
        }

        public string ld(whd_d_fk fk, int state, int ACID)
        {
            string r = "";
            using (TransactionScope tc = new TransactionScope())
            {
                try
                {
                    using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                    {
                        
                        ctx.whd_d_fk.Delete(p => p.did == fk.did);
                        whd_d_fk_Add(fk, ACID, ctx);
                        ctx.whd_d.Single(p=>p.did==fk.did).state=state;
                        ctx.SubmitChanges();
                        tc.Complete();

                    }
                }
                catch (Exception ex)
                {
                    r = ex.Message;
                }
            }
            return r;
        }

        public List<get_whd_View_allResult> getwhd_view_allresult(string ldrname,int state,int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                Expression<Func<get_whd_View_allResult, bool>> predicate = PredicateBuilder.True<get_whd_View_allResult>();


               if (!string.IsNullOrEmpty(ldrname))
               {
                   predicate = predicate.And(p => p.录单人 == ldrname);
               }
               return ctx.get_whd_View_all(state).Where(p=>p.录单人==ldrname).ToList();
            }
        }

        public List<get_whd_View_all_bydateResult> get_whd_View_all_bydateResult(int state, DateTime sd, DateTime ed, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                return ctx.get_whd_View_all_bydate(state, sd, ed).ToList();
            }
        }
    }
}

